package it.gtd.swimpool.controller.resources;

import it.gtd.swimpool.model.business.AdministrationManager;
import it.gtd.swimpool.model.business.CommunicationManager;
import it.gtd.swimpool.model.entity.Administrator;
import it.gtd.swimpool.model.entity.User;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.*;

/*        _______  _______  _______             _        _______
|\     /|(  ___  )(  ____ )(  ____ )|\     /|   ( (    /|(  ____ \|\     /|
| )   ( || (   ) || (    )|| (    )|( \   / )   |  \  ( || (    \/| )   ( |
| (___) || (___) || (____)|| (____)| \ (_) /    |   \ | || (__    | | _ | |
|  ___  ||  ___  ||  _____)|  _____)  \   /     | (\ \) ||  __)   | |( )| |
| (   ) || (   ) || (      | (         ) (      | | \   || (      | || || |
| )   ( || )   ( || )      | )         | |      | )  \  || (____/\| () () |
|/     \||/     \||/       |/          \_/      |/    )_)(_______/(_______)

          _______  _______  _______  _
|\     /|(  ____ \(  ___  )(  ____ )( )
( \   / )| (    \/| (   ) || (    )|| |
 \ (_) / | (__    | (___) || (____)|| |
  \   /  |  __)   |  ___  ||     __)| |
   ) (   | (      | (   ) || (\ (   (_)
   | |   | (____/\| )   ( || ) \ \__ _
   \_/   (_______/|/     \||/   \__/(_)

                                    .-.           .-----.    .-.
                                    : :           `-. .-'   .' `.
                                    : `-. .-..-.    : : .--.`. .'.--. ,-.,-.,-.
                                    ' .; :: :; :    : :' .; :: :' '_.': ,. ,. :
                                    `.__.'`._. ;    :_;`.__.':_;`.__.':_;:_;:_;
                                           .-. :
                                           `._.'
*/

@Stateless
@Path("/")
public class SuperUserResource extends SecuredResource {
    private static final String EMAIL_PASSCHANGE_SUBJECT = "Your password has been reset!";
    private static final String EMAIL_PASSCHANGE_BODY = "A super user changed your password. Your new password is %s.";
    private static final String EMAIL_DELETION_SUBJECT = "Your account has been deleted";
    private static final String EMAIL_DELETION_BODY = "Your account has been deleted by a super user. For more information on this decision, please, contact %s";

    @PersistenceContext
    private EntityManager entityManager;

    @EJB
    private AdministrationManager administrationManager;

    @EJB
    private CommunicationManager communicationManager;

    @PUT
    @Path("/users/{id}/password")
    public void forcePasswordChange(
        @CookieParam(Cookie.SESSION_ID) String sessionId,
        @PathParam("id") int id,
        String password
    ) {
        checkSuperUser(sessionId);

        User target = entityManager.find(User.class, id);
        target.setPassword(password);

        communicationManager.sendEmail(target, EMAIL_PASSCHANGE_SUBJECT, String.format(EMAIL_PASSCHANGE_BODY, password));
    }

    @PUT
    @Path("/users/{id}")
    @Consumes("application/json")
    public void editUserProfile(
        @CookieParam(Cookie.SESSION_ID) String sessionId,
        @PathParam("id") int id,
        User user
    ) {
        checkSuperUser(sessionId);

        User target = entityManager.find(User.class, id);

        if (user.getDescription() != null)
            target.setDescription(user.getDescription());

        if (user.getEmail() != null)
            target.setEmail(user.getEmail());

        if (user.getInformation() != null)
            target.setInformation(user.getInformation());

        if (user.getName() != null)
            target.setName(user.getName());
    }

    @DELETE
    @Path("/users/{id}/avatar")
    public void deleteUserProfile(@CookieParam(Cookie.SESSION_ID) String sessionId, @PathParam("id") int id) {
        checkSuperUser(sessionId);

        User target = entityManager.find(User.class, id);
        target.deleteAvatar();
    }

    @DELETE
    @Path("/users/{id}")
    public void deleteUser(@CookieParam(Cookie.SESSION_ID) String sessionId, @PathParam("id") int id) {
        User superUser = checkSuperUser(sessionId);
        User target = entityManager.find(User.class, id);

        communicationManager.sendEmail(target, EMAIL_DELETION_SUBJECT, String.format(EMAIL_DELETION_BODY, superUser.getEmail()));

        entityManager.remove(target);
    }

    @DELETE
    @Path("/admins/{id}")
    public void demoteAdmin(@CookieParam(Cookie.SESSION_ID) String sessionId, @PathParam("id") int id) {
        checkSuperUser(sessionId);

        Administrator target = entityManager.find(Administrator.class, id);

        administrationManager.demote(target);
    }
}
